﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shop.Bll.cost
{
    public class PostCalcStrategyBll
    {
        public async Task<List<PostCalcStrategy>> Query(string find)
        {
            return await DB.FSql.Select<PostCalcStrategy>()
                .Where(a => a.isdel == false && a.isDetails== false)
                .WhereIf(!string.IsNullOrEmpty(find), a => a.Name == find)
                .ToListAsync();

        }

        public async Task<List<PostCalcStrategy>> GetItems(string name)
        {
            return await DB.FSql.Select<PostCalcStrategy>()
                .Where(a => a.isdel == false && a.Name== name && a.isDetails== true)
                .ToListAsync();

        }


        public async Task<Result> SaveItems(IEnumerable<PostCalcStrategy> items)
        {
            if(items == null)
            {
                return new Result(false, "系统错误");
            }


            foreach (var item in items)
            {
                item.isdel = false;
            }

            await DB.FSql.InsertOrUpdate<PostCalcStrategy>()
                .SetSource(items)
                .ExecuteAffrowsAsync();
            return new Result(true);
        }


        public async Task<Result> DeleteItems(params int[] items)
        {
            if (items == null)
            {
                return new Result(false, "系统错误");
            }

            await DB.FSql.Update<PostCalcStrategy>()
                .Where(a=> items.Contains(a.id))
                .Set(a=> a.isdel, true)
                .ExecuteAffrowsAsync();

            return new Result(true);
        }

        public async Task<Result> DeleteItems(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return new Result(false, "系统错误");
            }

            await DB.FSql.Update<PostCalcStrategy>()
                .Where(a => a.Name== name)
                .Set(a => a.isdel, true)
                .ExecuteAffrowsAsync();

            return new Result(true);
        }


        /// <summary> 查所有物流公司 </summary>
        public async Task<List<string>> QueryAllLogis(string find)
        {
            var list = await DB.FSql.Select<PostPrice>()
            .Where(a => a.isdel == false)
            .WhereIf(!string.IsNullOrEmpty(find), a=> a.logistics_name.Contains(find))
            .Distinct()
            .ToListAsync(a => a.logistics_name);

            return list;
        }


        public async Task<List<LogiCalc>> QueryLogiCalcs()
        {
            var list = await DB.FSql.Select<LogiCalc>()
            .ToListAsync();

            return list;
        }



        public async Task SetLogiCalc(string logi, string calc)
        {
            var find = await DB.FSql.Select<LogiCalc>()
                .Where(a => a.Logi == logi)
                .ToOneAsync();
            if (find == null)
            {
                await DB.FSql.Insert(new LogiCalc() { CalcMethod=calc, Logi= logi })
                    .ExecuteAffrowsAsync();
            }
            else
            {
                await DB.FSql.Update<LogiCalc>()
                    .Where(a => a.id == find.id)
                    .Set(a => a.CalcMethod, calc)
                    .ExecuteAffrowsAsync();
            }
        }
    }
}
